<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="目标字段转换规则" @del="del" @newCreate="newCreate" @prequeryData="prequeryData"></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="apiMap.getListApi" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
            @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'目标字段转换规则'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>


    <el-dialog title="预览转换数据集" :visible.sync="dialogTableVisible" width="720px">
      <div v-tit="'预览转换数据集'">
          <el-input  v-model="querysql"  :disabled="true" type ="textarea"> </el-input>

        <el-table :data="gridData">
        <el-table-column property="source" label="转换源值" width="150"></el-table-column>
        <el-table-column property="target" label="转换目标值" width="150"></el-table-column>
       </el-table>
      </div>
  </el-dialog>
  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle'

export default {
  name: 'serialrule',
  data() {
    return {
      dialogTableVisible:false,
      querysql:'',
      tableSelectData: [],
      gridData:[],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        id: null,
        name: '',
        code: '',
        type: ''
      },
      options: {
        datasources: []
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          disabled: true,
          invalid: 'del'
        },{
          label:'预览转换数据集',
          disabled:true,
          invalid:'prequeryData'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: '目标字段转换规则名称',
          prop: 'name',
          input: 'input'
        },
        {
          label: '类型',
          prop: 'type',
          input: 'select',
          options: [
          { label: '系统代码', value: 'codelkup' },
          { label: '自定义表', value: 'table' }
          ]
        },
        {
          label: '数据源',
          prop: 'datasource',
          input: 'input'
        },
        {
          label: '系统代码名称',
          prop: 'listname',
          input: 'input'
        },
        {
          label: '数据表名称',
          prop: 'tablename',
          input: 'input'
        },
         {
          label: '目标值',
          prop: 'targetfield',
          input: 'input'
        },
         {
          label: '源值',
          prop: 'sourcetablecolumn',
          input: 'input'
        },
         {
          label: '自定义查询条件',
          prop: 'externquerycondition',
          input: 'input'
        },
        {
          label: '结果集合中不存在是否报错',
          prop: 'errornulltargetfield',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        // {
        //   label: '结果集中存在多个匹配数据是否报错',
        //   prop: 'errormultipletarget',
        //   input: 'select',
        //   options: [
        //   { label: '是', value: 'true' },
        //   { label: '否', value: 'false' }
        //   ]
        // },
      ]
    },
    formConfig() {
      return [
        {
          label: '目标字段转换规则名称',
          prop: 'name',
          type: 'input'
        },
        {
          label: '类型',
          prop: 'type',
          type: 'select',
          options: [
          { label: '系统代码', value: 'codelkup' },
          { label: '自定义表', value: 'table' }
          ]
        },
        {
          label: '数据源',
          prop: 'datasource',
          type: 'select',
          options:this.options.datasources
        }, 
        {
          label: '系统代码名称',
          prop: 'listname',
          type: 'input'
        }, 
        {
          label: '数据表名称',
          prop: 'tablename',
          type: 'input'
        },
        {
          label: '目标值',
          prop: 'targetfield',
          type: 'input'
        },
        {
          label: '源值',
          prop: 'sourcetablecolumn',
          type: 'input'
        },
        {
          label: '自定义查询条件',
          prop: 'externquerycondition',
          type: 'input'
        },
        {
          label: '结果集合中不存在是否报错',
          prop: 'errornulltargetfield',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        // {
        //   label: '结果集中存在多个匹配数据是否报错',
        //   prop: 'errormultipletarget',
        //   type: 'select',
        //   options: [
        //   { label: '是', value: 'true' },
        //   { label: '否', value: 'false' }
        //   ]
        // }

      ]
    },
    formRules() {
      return {

      }
    },
    apiMap() {
      return {
        getListApi: '/v1/targetFieldConvertRule/selectTargetFieldConvertRulePageList',
        getInfoApi:'/v1/targetFieldConvertRule/selectPlTargetFieldConvertRuleById',
        editApi:'/v1/targetFieldConvertRule/editTargetFieldConvertRule',
        deleteApi: '/v1/targetFieldConvertRule/deleteTargetFieldConvertRuleIds',
        addApi: '/v1/targetFieldConvertRule/addTargetFieldConvertRule'
      }
    }
  },
  methods: {
    prequeryData(data){
      if(data.length != 1){
        return this.$message.error("请选择一条数据进行操作");
      }
      this.dialogTableVisible = true;
      this.$post('/v1/targetFieldConvertRule/previewQuerySql',data[0]).then(res => { 
        this.gridData = res.data.item;
      })
      this.querysql = data[0].querymappingsql;
    },
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)

        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(this.apiMap.editApi, params)
        } else {
          promise = this.$post(this.apiMap.addApi, params)
        }
        promise.then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
          this.$refs.detail?.resetFields()
          this.dialogModule.flag = false
        })
      })

    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, {
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.name}]目标字段转换规则`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增目标字段转换规则'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post(this.apiMap.deleteApi, data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    getOptions() {
      this.$post('/v1/targetFieldConvertRule/queryActiveWhseid',{}).then(res => {
        this.options.datasources = res.data.item;
      })
    },

    closed() {
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
  },
  mounted() {
    this.getOptions();
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
